//
//  MYEmoticonToolBar.swift
//  NewMicroblog
//
//  Created by apple on 2016/11/2.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit


class MYEmoticonToolBar: UIStackView {

    var lastSelectedBtn: UIButton?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    ///  搭建主界面
    private func  setupUI(){
        axis = .horizontal
        distribution = .fillEqually
        addChildButton(imageName: "compose_emotion_table_left", title: "最近")
        addChildButton(imageName: "compose_emotion_table_mid", title: "默认")
        addChildButton(imageName: "compose_emotion_table_mid", title: "Emoji")
        addChildButton(imageName: "compose_emotion_table_right", title: "浪小花")

    }


    ///添加字按钮操作
    private func addChildButton(imageName: String, title: String){
         let btn = UIButton()
        btn.addTarget(self, action: #selector(buttonAction(btn:)), for: .touchUpInside)
        btn.setBackgroundImage(UIImage(named:imageName + "_normal"), for: .normal)
        btn.setBackgroundImage(UIImage(named:imageName + "_selected"), for: .selected)
        btn.setTitleColor(UIColor.white, for: .normal)
        btn.setTitleColor(UIColor.gray, for: .selected)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        btn.setTitle(title, for: .normal)
        btn.adjustsImageWhenHighlighted = false
        addArrangedSubview(btn)
    }

    @objc private func buttonAction(btn: UIButton){
        if lastSelectedBtn == btn {
            return
        }
        lastSelectedBtn?.isSelected = false
        btn.isSelected = true
        lastSelectedBtn = btn
    }

}

